<?php
require_once('internal/IDatabase.interface.php');
/*
mae. 1.0
MySQLi database support
*/

class Database implements IDatabase {
	public $affected_rows, $num_rows;

	function __construct($server, $user, $pass, $database) {
		$this->db = @new mysqli($server, $user, $pass, $database);
		if (!$this->db) error('db_con');
	}

	function __destruct() {
		return $this->db->close();
	}

	public function query($que) {
		$q = $this->db->query($que);
		$this->affected_rows = $this->db->affected_rows;
		if (strpos(strtolower($que), 'select') === 0) $this->num_rows = $q->num_rows;
		return $q;
	}

	public function fetch_assoc($res) {
		return $res->fetch_assoc();
	}

	public function error() {
		return $this->db->error;
	}

	public function fetch_row($res) {
		return $res->fetch_row();
	}

	public function fetch_array($res) {
		return $res->fetch_array();
	}

	public function select($database, $what='*', $where=array()) {
		global $mae;
		$wheres = (count($where)==0) ? '' : ' WHERE '.implode(', ', $where);
		return $this->query("SELECT $what FROM {$mae[db_prefix]}$database$wheres;");
	}

}

?>
